<?php
/**
 * Created by PhpStorm.
 * User: iwm
 * Date: 2017/2/7
 * Time: 16:00
 */

namespace app\home\controller;

use app\api\model\dao\GoodsBrands;
use app\api\model\dao\GoodsCategory;
use app\api\model\dao\PromotionRegion;
use app\api\model\dao\PromotionRegionGoods;
use app\api\model\page\goods\GetGoods;
use app\api\model\page\goods\GetGoodsCategory;
use app\home\model\page\goods\AddGoods;
use app\home\model\page\goods\AddGoodsAttr;
use app\home\model\page\goods\AddGoodsAttribute;
use app\home\model\page\goods\DeleteGoods;
use app\home\model\page\goods\UpdateGoodsAttr;
use app\home\model\page\goods\UpdateGoodsAttribute;
use app\home\model\page\goods\DeleteGoodsAttr;
use app\home\model\page\goods\DeleteGoodsAttribute;
use app\home\model\page\goods\GetGoodsList;
use app\home\model\page\shop\AddBanner;
use app\home\model\page\shop\AddFlashSale;
use app\home\model\page\shop\AddNav;
use app\home\model\page\shop\AddPromotionRegion;
use app\home\model\page\shop\AddPromotionRegionGoods;
use app\home\model\page\shop\DeleteBrand;
use app\home\model\page\shop\DeleteFlashSale;
use app\home\model\page\shop\DeletePromotionRegion;
use app\home\model\page\shop\EditBrand;
use app\home\model\page\shop\GeRegionInfo;
use app\home\model\page\shop\GetAd;
use app\home\model\page\shop\GetAdPage;
use app\home\model\page\shop\GetBanner;
use app\home\model\page\shop\GetBannerImage;
use app\home\model\page\shop\GetBannerImageList;
use app\home\model\page\shop\GetCategoryInfo;
use app\home\model\page\shop\GetFlashSalePage;
use app\home\model\page\shop\GetParentRegionInfo;
use app\home\model\page\shop\GetRegionInfo;
use app\home\model\page\shop\UpdateFlashSale;
use app\home\model\page\shop\UpdateNav;
use app\home\model\page\shop\UpdateBanner;
use app\home\model\page\shop\AddBannerImage;
use app\home\model\page\shop\UpdateBannerImage;
use app\home\model\page\shop\DeleteBannerImage;
use app\home\model\page\shop\AddBrand;
use app\home\model\page\shop\BrandLists;
use app\home\model\page\shop\GetCategoryList;
use app\home\model\page\shop\AddAd;
use app\home\model\page\shop\AddAdPosition;
use app\home\model\page\shop\UpdateAd;
use app\home\model\page\shop\UpdateAdPosition;
use app\home\model\page\shop\UpdatePromotionRegion;
use app\library\PageBase;
use think\Controller;
use think\Request;

class Shop extends Controller
{
    private $param;

    public function __construct()
    {
        parent::__construct();
        $this->param = Request::instance()->param();
    }

    public function addGoods()
    {
        $obj = new AddGoods();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function deleteGoods()
    {
        $obj = new DeleteGoods();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addGoodsPage()
    {

        $objBrand = new GoodsBrands();
        $brandList = $objBrand->getBrandNameList();
        $data['brand'] = $brandList;
        //return JSON($brandList);
        $this->assign('data', $data);

        return $this->fetch('home/pages/shop/addgoods');
    }

    public function GoodsDetailPage()
    {

        if (isset($this->param['type']) && $this->param['type'] == 'detail') {
            $obj = new GetGoods();
            $ret = $obj->excute($this->param);
            if ($ret['errno']) {
                return JSON($ret);
            }
            $ret['data']['test'] = 'test';
            $this->assign('data', $ret['data']);
            //return JSON(($ret['data']));
            return $this->fetch('home/pages/shop/goodsdetail');
        } else {

            //页面品牌id与信息
            return $this->fetch('home/pages/shop/addgoods');
        }

    }

    public function goodsListPage()
    {
        $obj = new GetGoodsList();
        $data = $obj->excute($this->param);
        //$this->assign('data',$ret['data']);
        //return JSON(($data));
        //$data['name'] = 'ThinkPHP';
        //$data['email'] = 'thinkphp@qq.com';
        $this->assign('data', $data['data']);
        return $this->fetch('home/pages/shop/goodslist');
    }

    public function getGoodsList()
    {
        $obj = new GetGoodsList();
        $orderInfo = $obj->excute($this->param);
        return JSON($orderInfo);
    }

    public function getBrandList()
    {
        $obj = new BrandLists();
        $ret = $obj->excute($this->param);
        //if(isset($ret['errno'])){
        //return header('Location:/home/loginpage');
        //}
        //return JSON(var_dump($ret));
        $this->assign('data', $ret['data']);
        return $this->fetch('home/pages/shop/brandlist');
    }

    public function addBrand()
    {
        $obj = new AddBrand();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function delBrand()
    {
        $obj = new DeleteBrand();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function editBrand()
    {
        $obj = new EditBrand();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getCateGoryList()
    {
        $obj = new GetCategoryList();
        $ret = $obj->excute($this->param);
        if (isset($ret['errno'])) {
            return header('Location:/home/loginpage');
        }
        $this->assign('data', $ret);
        return $this->fetch('home/pages/shop/categorylist');
    }

    public function getAllCategory()
    {
        $obj = new GetCategoryInfo();
        $catList = $obj->excute($this->param);
        //return JSON($catList);
        $return = [];
        foreach ($catList['data'] as $item) {
            if (isset($item['parent_id']) && $item['parent_id'] == 0) {
                $return[$item['cat_id']]['parent_cat'] = $item;
            } else {
                $return[$item['parent_id']]['sub_cat'][] = $item;
            }
        }
        //return JSON($return);
        $tmp = [];
        foreach ($return as $catInfo) {
            $sub = [];
            if (isset($catInfo['sub_cat'])) {
                foreach ($catInfo['sub_cat'] as $subCat) {
                    $id = $subCat['cat_id'];
                    $text = $subCat['cat_name'];
                    $sub[] = [
                        'id' => $id, 'text' => $text
                    ];
                }
                $tmp[] = [
                    'text' => $catInfo['parent_cat']['cat_name'],
                    'id' => $catInfo['parent_cat']['cat_id'],
                    'children' => $sub
                ];
            } else {
                $id = $catInfo['parent_cat']['cat_id'];
                $text = $catInfo['parent_cat']['cat_name'];
                $tmp[] = [
                    'id' => $id, 'text' => $text
                ];
            }
        }
        $data['errno'] = 0;
        $data['data'] = $tmp;
        return JSON($data);
    }

    public function couponList()
    {
        $obj = new PageBase();
        $ret = $obj->checkToken($this->param);
        if (false === $ret) {
            header('Location:/home/loginpage');
            exit;
        }
        return $this->fetch('home/pages/shop/couponlist');
    }

    public function addBanner()
    {
        $obj = new AddBanner();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getBanner()
    {
        $obj = new GetBanner();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getBannerPage()
    {
        $obj = new GetBanner();
        $ret = $obj->excute($this->param);
        $this->assign('data', $ret['data']);
        return $this->fetch('home/pages/management/bannerlist');
    }

    public function updateBanner()
    {
        $obj = new UpdateBanner();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addBannerImage()
    {
        $obj = new AddBannerImage();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getBannerImage()
    {
        $obj = new GetBannerImage();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getBannerImagePage()
    {
        $obj = new GetBannerImageList();
        $ret = $obj->excute($this->param);
        $this->assign('data', $ret['data']);
        //return JSON($ret['data']->all());
        return $this->fetch('home/pages/management/bannerimagelist');
    }

    public function updateBannerImage()
    {
        $obj = new UpdateBannerImage();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function deleteBannerImage()
    {
        $obj = new DeleteBannerImage();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addNav()
    {
        $obj = new AddNav();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function updateNav()
    {
        $obj = new UpdateNav();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getAd()
    {
        $obj = new GetAd();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getAdPage()
    {
        $obj = new GetAdPage();
        $data = $obj->excute($this->param);
        //return JSON($data);
        $this->assign('data', $data['data']);
        return $this->fetch('home/pages/management/bannerimagelist');
    }

    public function addAd()
    {
        $obj = new AddAd();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function updateAd()
    {
        $obj = new UpdateAd();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addAdPosition()
    {
        $obj = new AddAdPosition();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function updateAdPosition()
    {
        $obj = new UpdateAdPosition();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addGoodsAttr()
    {
        $obj = new AddGoodsAttr();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function addGoodsAttribute()
    {
        $obj = new AddGoodsAttribute();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function updateGoodsAttr()
    {
        $obj = new UpdateGoodsAttr();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function updateGoodsAttribute()
    {
        $obj = new UpdateGoodsAttribute();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function deleteGoodsAttr()
    {
        $obj = new DeleteGoodsAttr();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function deleteGoodsAttribute()
    {
        $obj = new DeleteGoodsAttribute();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getCategory()
    {
        $obj = new GetCategoryInfo();
        $catInfo = $obj->excute($this->param);
        //return JSON(gettype($catInfo));
        $tmp[] = ['text' => '无', 'id' => '0'];
        foreach ($catInfo['data'] as $info) {
            if (!$info['parent_id']) {
                $tmp[] = [
                    'text' => $info['cat_name'],
                    'id' => $info['cat_id'],
                ];
            }
        }
        /*
        $tmp = [];
        foreach($info['data'] as $catInfo){
            if(isset($catInfo['sub_cat']) && !empty($this->param['sub'])){
                foreach($catInfo['sub_cat'] as $subCat){
                    //var_dump($subCat);
                    $id = $subCat['cat_id'];
                    $text = $subCat['cat_name'];
                    $sub[] = [
                        'id'=>$id,'text'=>$text
                    ];
                }
                $tmp[] = [
                    'text' => $catInfo['parent_cat']['cat_name'],
                    'id' => $catInfo['parent_cat']['cat_id'],
                    'children' => $sub
                ];
            }else{
                $id = $catInfo['parent_cat']['cat_id'];
                $text = $catInfo['parent_cat']['cat_name'];
                $tmp[] = [
                    'id'=>$id,'text'=>$text
                ];
            }
        }
        $info['data'] = $tmp;
        */
        //$catInfo = $tmp;
        $catInfo['data'] = $tmp;
        return JSON($catInfo);
    }

    /**
     * 首页专区
     */
    public function indexRegionList()
    {
        $obj = new PageBase();
        $ret = $obj->checkToken($this->param);
        if (false === $ret) {
            header('Location:/home/loginpage');
            exit;
        }
        $obj = new PromotionRegion();
        $list = $obj->getPromotionRegionList();
        //return JSON($list);
        $this->assign('data', $list);
        return $this->fetch('home/pages/shop/indexregion');

    }

    /**
     * 首页专区子分区
     */
    public function getRegionOrder()
    {

        $obj = new GetGoodsList();
        $data = $obj->excute($this->param);

        //return JSON($data);
        $obj = new PromotionRegion();

        $list = $obj->getRegionOrder();
        //return JSON($list);
        /*
         return JSON($ret);
        foreach($ret as $value){
            //$temp[] = $value->PromotionRegionGoods;
        }
        return JSON($temp);
        */
        $this->assign('data', $list);
        return $this->fetch('home/pages/shop/getregionorder');
    }

    /**
     * 增加专区或者子专区
     */
    public function addIndexRegion()
    {
        $obj = new AddPromotionRegion();
        $ret = $obj->excute($this->param);
        return JSON($ret);
    }

    /**
     * @return \think\response\Json
     * 删除分区
     */
    public function deleteIndexRegion()
    {
        $obj = new DeletePromotionRegion();
        $ret = $obj->excute($this->param);
        return JSON($ret);
    }

    /**
     * @return \think\response\Json
     * 编辑分区
     */
    public function editIndexRegion()
    {
        $obj = new UpdatePromotionRegion();
        $ret = $obj->excute($this->param);
        return JSON($ret);
    }

    /**
     * 首页专区子分区内的商品
     */
    public function addSubRegionGoods()
    {
        $obj = new AddPromotionRegionGoods();
        $ret = $obj->excute($this->param);
        return JSON($ret);
    }


    public function getReionInfo()
    {

        /*
        $obj = new PromotionRegion();

        $ret = $obj->getRegionOrder();
        return JSON($ret);
        foreach($ret as $value){
            $temp[] = $value->PromotionRegionGoods;
        }
        return JSON($temp);

*/
        $obj = new GetRegionInfo();
        $data = $obj->excute($this->param);
        return JSON($data);
    }

    public function getFlashSalePage()
    {
        $obj = new GetFlashSalePage();
        $data = $obj->excute($this->param);
        if (isset($data['errno'])) {
            return header('Location:/home/loginpage');
        }
        //return JSON($data);
        $this->assign('data', $data);
        return $this->fetch('home/pages/shop/flashsale');
    }

    public function addFlashSale()
    {
        $obj = new AddFlashSale();
        $ret = $obj->excute($this->param);
        return JSON($ret);
    }

    public function delFlashSale()
    {
        $obj = new DeleteFlashSale();
        $ret = $obj->excute($this->param);
        return JSON($ret);

    }

    public function updateFlashSale()
    {
        $obj = new UpdateFlashSale();
        $ret = $obj->excute($this->param);
        return JSON($ret);

    }

}